var PATH_PREFIX ="";

function setPathPrefix(path){
	PATH_PREFIX =  path;
}
$(function() {
	// 注册清空事件
    WASP_WIDGET.registerResetClearEvent();
    // 给select2组件加载样式
    WASP_WIDGET.initializeSelectStyle();
});

var $invesSecurList = $('#invesSecurList');

$(function() {
	$invesSecurList.jqGrid({   
		url:PATH_PREFIX+'investTradeListPage.do',    
		//caption:'投资标的列表<button type="button" class="btn btn-default btn-sm btn-small btn-fullscreen" title="" tabindex="-1"><i class="fa fa-arrows-alt icon-fullscreen"></i></button>',  
		datatype: "json",
		postData:{
			'sp[securName]' : $("#securName").val(),
			'sp[securType]' : $("#securType").val()
		},
		colNames:["主键","标的代码","标的名称","标的类型","补充信息","标的总份额","份额合计可投资比例","份额单产品可投资比例","标的净值文件位置","标的申购上限","标的认购上限","标的最小认购额","操作"],        
		colModel:[  { name: 'investSecurId', index: 'investSecurId', hidden: true, key: true, resizable: true, sortable: false },       
		            { name: 'securCode', index: 'securCode', resizable: true, sortable: false },
		            { name: 'securName', index: 'securName', resizable: true, sortable: false },
		            { name: 'securType', index: 'securType', resizable: true, sortable: false ,
		            	formatter:function(cellvalue, options, rowObject){
		            		if(cellvalue =="01"){
		            			return "股票";
		            		}else if(cellvalue =="02"){
		            			return "债券";
		            		}else if(cellvalue =="03"){
		            			return "基金";
		            		}else if(cellvalue =="04"){
		            			return "回购";
		            		}else if(cellvalue =="05"){
		            			return "管理人";
		            		}else if(cellvalue =="06"){
		            			return "资管计划";
		            		}else if(cellvalue =="07"){
		            			return "信托";
		            		}else if(cellvalue =="08"){
		            			return "年金";
		            		}else{
		            			return cellvalue;
		            		}
		            	}
		            },
		            { name: 'supplyInfo', index: 'supplyInfo', resizable: true, sortable: false },
		            { name: 'securTotalShare', index: 'securTotalShare', resizable: true, sortable: false },
		            { name: 'shareSumProportion', index: 'shareSumProportion', width:200, resizable: true, sortable: false },
		            { name: 'shareSingleProportion', index: 'shareSingleProportion', width:200, resizable: true, sortable: false },
		            { name: 'securFilePath', index: 'securFilePath', resizable: true, sortable: false },
		            { name: 'purchaseLimit', index: 'purchaseLimit', resizable: true, sortable: false },
		            { name: 'subscribeLimit', index: 'subscribeLimit', resizable: true, sortable: false },
		            { name: 'minSubscribeShare', index: 'minSubscribeShare', resizable: true, sortable: false },
		            { name: 'option', index: 'option', width:300, resizable: true, sortable: false }
				  ],
		rowNum: 20,
        rowList: [20, 30, 50],
        rownumbers: true,
        rownumWidth: 50,
        prmNames: { search: "search", page: "pageNo", rows: "limit" },
        height: $(window).height()-306,
        width: false,
        autowidth:true,
        editurl: '',
        viewrecords: true,
        cellEdit: false,
        shrinkToFit: true,
        grouping: false,
        jsonReader: {
            root: "items", //结果集
            records: "total", //总记录数 
            total: "pageCount", //总页数
            page: "pageNo", //当前页 
            repeatitems: false // (4) 
        },  
		multiselect:false,
		pager: "#invesSecurListPage",						
		viewrecords: true,
		hidegrid: false,
		gridComplete: function(){
			var ids = $invesSecurList.jqGrid('getDataIDs');
			for (var i = 0; i < ids.length; i++) {
				var rowId = ids[i];
				detailBtn = '<a class="btn btn-link btn-jqgrid" title="详情" onclick="detailView(\'' + rowId + '\');" >详情</a><span class="split"></span>';
				updateBtn = '<a class="btn btn-link btn-jqgrid" title="修改" onclick="updateView(\'' + rowId + '\');" >修改</a><span class="split"></span>';
				deleteBtn = '<a class="btn btn-link btn-jqgrid" title="删除" onclick="deleteView(\'' + rowId + '\');" >删除</a>';
				$invesSecurList.jqGrid('setRowData', ids[i], {
					option : detailBtn + updateBtn + deleteBtn
				});
			}
		}
	});
	$invesSecurList.navGrid('#invesSecurListPage',{edit:false,add:false,del:false,search:false,refreshstate:'current'});
	$invesSecurList.jqGrid('setFrozenColumns');
	jqGridResize($invesSecurList);
	$(".ui-jqgrid .ui-jqgrid-htable").css("width",$(".ui-jqgrid").width()-1);
	$(".ui-jqgrid .ui-jqgrid-btable").css("width",$(".ui-jqgrid").width()-1);
});

/**
 * 查询按钮
 * 根据条件查询数据
 * @param flag
 */
function queryByCondtion(flag){
    var postData = $invesSecurList.jqGrid("getGridParam", "postData");
    $.extend(postData, {
		'sp[securName]' : $("#securName").val(),
		'sp[securType]' : $("#securType").val()
	});
    if (flag) {
    	$invesSecurList.jqGrid("setGridParam",{"datatype": "json"}).trigger("reloadGrid",[{page:1}]);//重新载入Grid表格
    } else {
    	$invesSecurList.jqGrid("setGridParam",{"datatype": "json"}).trigger("reloadGrid");//重新载入Grid表格
    }
}

/**
 * 跳转投资标的新增页面
 * @returns
 */
function invesSecurAddView(){
	/** 新建 */
	 var url = PATH_PREFIX+"investTradeAddView.do";
	 window.open(url);
}

function updateView(id){
	/** 新建 */
	 var url = PATH_PREFIX+"investTradeUpdateView.do?id="+id;
	 window.open(url);
}

function detailView(id){
	/** 新建 */
	var url = PATH_PREFIX+"investTradeDetailView.do?id="+id;
	window.open(url);
}

function deleteView(id){
	ctools.confirm({text:"是否确认删除投资标的信息？"},function(isConfirm){
		if(isConfirm){
			if (id == null || id == "") {
				return;
			}
			var actionUrl= PATH_PREFIX + "deleteInvesSecur.do";
			$.ajax({
				type: 'POST',
				url: actionUrl,
				dataType:'json',
				data:{
					"id" : id
				},
				success: function(data){
					if(data.resultCode == "0000"){
						ctools.alert_sweet('投资标的信息删除成功！', "success", "" , function(){
							queryByCondtion(true);
						});
					}else{
						ctools.alert_sweet('投资标的信息删除失败！', "error", "失败原因："+data.resultMsg);
					}
				},
				error:function(xhr){
					switch(xhr.status){
						case 403:sweetAlert("对不起，您无此权限！","","error");break;
						case 404:sweetAlert("对不起，无此页面！", "","error");break;
						case 500:sweetAlert("内部错误，请联系管理员！","","error");break;  
						case 504:sweetAlert("超时，请联系管理员！", "","error");break;  
						case 417:sweetAlert("内部错误，请联系管理员！", "","error");break;  
					}
				}
			});
		}
	});
}
